package com.usian.behavior.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.behavior.mapper.ApBehaviorEntryMapper;
import com.usian.behavior.mapper.ApReadBehaviorMapper;
import com.usian.behavior.service.ApBehaviorEntryService;
import com.usian.behavior.service.ApReadBehaviorService;
import com.usian.model.article.dtos.ArticleInfoDto;
import com.usian.model.behavior.dtos.ReadBehaviorDto;
import com.usian.model.behavior.pojos.ApBehaviorEntry;
import com.usian.model.behavior.pojos.ApReadBehavior;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.user.pojos.ApUser;
import com.usian.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * ClassName: ApReadBehaviorService
 * Author:   WGG
 * Date:    2022-12-2022/12/1-15:32
 * Version: 1.0
 * Description:
 */
@Service
public class ApReadBehaviorServiceImpl extends ServiceImpl<ApReadBehaviorMapper,ApReadBehavior> implements ApReadBehaviorService {


    @Resource
    private ApBehaviorEntryService apBehaviorEntryService;

    @Override
    public ResponseResult readBehavior(ReadBehaviorDto dto) {
        //判断是否为空
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //拿到登录id
        ApUser user = AppThreadLocalUtils.getUser();
        if (user.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.TOKEN_EXPIRE);
        }
        //根据登录id判断行为实体是否存在
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.showBehavior(user.getId(),null);
        if (apBehaviorEntry==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //查询阅读行为数据是否存在
        ApReadBehavior readBehavior = getOne(Wrappers.<ApReadBehavior>lambdaQuery().eq(ApReadBehavior::getEntryId, apBehaviorEntry.getId()));

        if (readBehavior==null){
            //封装数据
            ApReadBehavior apReadBehavior = new ApReadBehavior();
            apReadBehavior.setArticleId(dto.getArticleId());
            apReadBehavior.setCount(dto.getCount());
            apReadBehavior.setCreatedTime(new Date());
            apReadBehavior.setEntryId(apBehaviorEntry.getId());
            apReadBehavior.setLoadDuration(dto.getLoadDuration());
            apReadBehavior.setPercentage(dto.getPercentage());
            apReadBehavior.setReadDuration(dto.getReadDuration());
            apReadBehavior.setUpdatedTime(new Date());
            //添加
            save(apReadBehavior);
        }else {
            //查询当前阅读的数量
            Integer count = readBehavior.getCount();
            //计算阅读数量
            count+=dto.getCount();
           //修改
            update(Wrappers.<ApReadBehavior>lambdaUpdate()
                    .eq(ApReadBehavior::getId,readBehavior.getId())
                    .set(ApReadBehavior::getCount,count)
                    .set(ApReadBehavior::getLoadDuration,dto.getLoadDuration())
                    .set(ApReadBehavior::getReadDuration,dto.getReadDuration())
                    .set(ApReadBehavior::getPercentage,dto.getPercentage())
            );
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ApReadBehavior showReadBehavior(ArticleInfoDto dto) {
        if (dto != null){
            return getOne(Wrappers.<ApReadBehavior>lambdaQuery().eq(ApReadBehavior::getArticleId,dto.getArticleId()).eq(ApReadBehavior::getEntryId,dto.getId()));
        }
        return null;
    }
}
